package com.github.hwangrq.minicat.server;

import java.io.IOException;
import java.io.InputStream;

/**
 * @author hwangrq
 * @date 2021/3/16 22:05
 * @description 把请求信息封装为Request对象 （from inputStream）
 */
public class Request {

    /** 请求方式 get/post */
    private String method;

    /** /, /index.html */
    private String url;

    /** 输入流，其他属性从输入流中解析出来 */
    private InputStream inputStream;

    public Request() {}

    /**
     * 构造器
     * @param inputStream 输入流
     */
    public Request(InputStream inputStream) throws IOException {
        this.inputStream = inputStream;
        int count = 0;
        while (count == 0) {
            count = inputStream.available();
        }
        byte[] bytes = new byte[count];
        inputStream.read(bytes);
        String inputStr = new String(bytes);
        // 获取第一行请求头信息  GET / HTTP/1.1
        String firstLineStr = inputStr.split("\\n")[0];
        String[] strings = firstLineStr.split(" ");
        this.method = strings[0];
        this.url = strings[1];
        System.out.println("method: " + method);
        System.out.println("url: " + url);
    }

    public String getMethod() {
        return method;
    }

    public void setMethod(String method) {
        this.method = method;
    }

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    public InputStream getInputStream() {
        return inputStream;
    }

    public void setInputStream(InputStream inputStream) {
        this.inputStream = inputStream;
    }
}
